package name.zxw.datastructures.queue;

import java.util.Arrays;

/**
 * 环形队列
 *
 * @author ZhangXinWang
 * @date 2022/5/8 15:49
 */
public class CircleArray {

    /*帮助理解
    环形队列,自己画一个环就理解了
     */

    private int maxSize;
    private int front;
    private int rear;
    private int[] arr;

    //队列构造器
    public CircleArray(int maxSize) {
        this.maxSize = maxSize;
        arr = new int[maxSize];
    }

    //队列满
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    //队列空
    public boolean isEmpty() {
        return front == rear;
    }

    //添加到队列
    public void addQueue(int i) {
        if (isFull()) {
            throw new RuntimeException("队列满");
        }
        arr[rear] = i;
        rear = (rear + 1) % maxSize;
    }

    //移除出队列
    public int getQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列空");
        }
        int cache = arr[front];
        front = (front + 1) % maxSize;
        return cache;
    }

    //显示队列
    public void showQueue() {
        System.out.println(Arrays.toString(arr));
    }

    //显示队列头(非取出头)
    public int headQueue() {
        if (isEmpty()) {
            throw new RuntimeException("队列空");
        }
        return arr[front];
    }


}
